package transport;

import java.util.HashMap;
import java.util.Map;

public class MonitorVehicleTracker {

	public final Map<String, MutablePoint> locations;

	public MonitorVehicleTracker(Map<String, MutablePoint> data) {
		locations = deepcopy(data);
	}

	public synchronized MutablePoint getLocation(String name) {
		MutablePoint loc = safeGet(name);
		return new MutablePoint(loc.x, loc.y);
	}

	public synchronized void setLocation(String name, MutablePoint point) {
		MutablePoint loc = safeGet(name);
		loc.x = point.x;
		loc.y = point.y;
	}

	private MutablePoint safeGet(String name) {
		MutablePoint loc = locations.get(name);
		if (name == null)
			throw new IllegalArgumentException();
		return loc;
	}

	public synchronized Map<String, MutablePoint> getLocations() {
		return deepcopy(locations);
	}

	private Map<String, MutablePoint> deepcopy(Map<String, MutablePoint> src) {
		Map<String, MutablePoint> copy = new HashMap<String, MutablePoint>();
		for (String id : src.keySet()) {
			MutablePoint point = src.get(id);
			copy.put(id, new MutablePoint(point));
		}
		return copy;
	}

}
